package com.tancire.web.controller.platform.system;

import com.tancire.common.core.controller.BaseController;
import com.tancire.common.core.domain.AjaxResult;
import com.tancire.common.core.domain.model.RegisterBody;
import com.tancire.common.enums.UserSource;
import com.tancire.common.utils.StringUtils;
import com.tancire.framework.web.service.HeaderService;
import com.tancire.framework.web.service.SysRegisterService;
import com.tancire.system.service.ISysConfigService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 注册验证
 *
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController {

    @Resource
    private SysRegisterService registerService;

    @Resource
    private ISysConfigService configService;

    @Resource
    private HeaderService headerService;

    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user) {

        int userSource = headerService.getCurrentUserSource();
        String msg = "";
        if (userSource == UserSource.SYSTEM.getCode()) {
            if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
                return error("当前系统没有开启注册功能！");
            }
            msg = registerService.register(user);
        } else if (UserSource.MEMBER.getCode() == userSource) {
            msg = registerService.register(user);
        } else {
            msg = "非法请求";
        }
        return StringUtils.isEmpty(msg) ? success("注册成功") : error(msg);
    }


}
